home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / TRCKR.PAK / TRACKVW.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  9KB  |  307 lines

  1. // trackvw.cpp : implementation of the CTrackerView class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13.  
  14. #include "stdafx.h"
  15. #include "trackapp.h"
  16.  
  17. #include "trackdoc.h"
  18. #include "trackvw.h"
  19. #include "dialogs.h"
  20.  
  21. #ifdef _DEBUG
  22. #undef THIS_FILE
  23. static char BASED_CODE THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CTrackerView
  28.  
  29. IMPLEMENT_DYNCREATE(CTrackerView, CView)
  30.  
  31. BEGIN_MESSAGE_MAP(CTrackerView, CView)
  32.     //{{AFX_MSG_MAP(CTrackerView)
  33.     ON_WM_LBUTTONDOWN()
  34.     ON_WM_SETCURSOR()
  35.     ON_COMMAND(ID_VIEW_SETHANDLESIZE, OnViewSethandlesize)
  36.     ON_COMMAND(ID_VIEW_SETMINIMUMSIZE, OnViewSetminimumsize)
  37.     //}}AFX_MSG_MAP
  38.     // Standard printing commands
  39.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  40.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  41. END_MESSAGE_MAP()
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CTrackerView construction/destruction
  45.  
  46. CTrackerView::CTrackerView()
  47. {
  48.     // TODO: add construction code here
  49. }
  50.  
  51. CTrackerView::~CTrackerView()
  52. {
  53. }
  54.  
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CTrackerView drawing
  57.  
  58. static void SetNormalRect(CRect& rect, int left, int top, int width, int height)
  59. {
  60.     // set it
  61.     rect.left = left;
  62.     rect.top = top;
  63.     rect.right = left + width;
  64.     rect.bottom = top + height;
  65.  
  66.     // normalize it
  67.     int nTemp;
  68.     if (rect.left > rect.right)
  69.     {
  70.         nTemp = rect.left;
  71.         rect.left = rect.right;
  72.         rect.right = nTemp;
  73.     }
  74.     if (rect.top > rect.bottom)
  75.     {
  76.         nTemp = rect.top;
  77.         rect.top = rect.bottom;
  78.         rect.bottom = nTemp;
  79.     }
  80. }
  81.  
  82. void CTrackerView::OnDraw(CDC* pDC)
  83. {
  84.     CTrackerDoc* pDoc = GetDocument();
  85.  
  86.     CBrush* pOldBrush = NULL;
  87.     TRY
  88.     {
  89.         // draw inside in various colors
  90.         CBrush brush1, brush2;
  91.         CRect rect;
  92.         int nWidth = pDoc->m_tracker.m_rect.Width();
  93.         int nHeight = pDoc->m_tracker.m_rect.Height();
  94.         int nSgnX = nWidth != 0 ? nWidth / abs(nWidth) : 1;
  95.         int nSgnY = nHeight != 0 ? nHeight / abs(nHeight) : 1;
  96.         pDC->SetTextAlign(TA_CENTER);
  97.         pDC->SetBkMode(TRANSPARENT);
  98.         int nCenterX, nCenterY;
  99.  
  100.         TEXTMETRIC tm;
  101.         pDC->GetTextMetrics(&tm);
  102.  
  103.         brush1.CreateSolidBrush(RGB(255, 0, 0));
  104.         pOldBrush = pDC->SelectObject(&brush1);
  105.         SetNormalRect(rect, pDoc->m_tracker.m_rect.left,
  106.             pDoc->m_tracker.m_rect.top, nWidth/2, nHeight/2);
  107.         pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
  108.         nCenterX = rect.left + rect.Width()/2;
  109.         nCenterY = rect.top + rect.Height()/2 - tm.tmHeight/2;
  110.         pDC->ExtTextOut(nCenterX, nCenterY, ETO_CLIPPED, rect, _T("1"), 1, NULL);
  111.  
  112.         brush2.CreateSolidBrush(RGB(0, 255, 0));
  113.         pDC->SelectObject(&brush2);
  114.         brush1.DeleteObject();
  115.         SetNormalRect(rect, pDoc->m_tracker.m_rect.left+nWidth/2,
  116.             pDoc->m_tracker.m_rect.top, (nWidth+nSgnX)/2, nHeight/2);
  117.         pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
  118.         nCenterX = rect.left + rect.Width()/2;
  119.         nCenterY = rect.top + rect.Height()/2 - tm.tmHeight/2;
  120.         pDC->ExtTextOut(nCenterX, nCenterY, ETO_CLIPPED, rect, _T("2"), 1, NULL);
  121.  
  122.         brush1.CreateSolidBrush(RGB(0, 0, 255));
  123.         pDC->SelectObject(&brush1);
  124.         brush2.DeleteObject();
  125.         SetNormalRect(rect, pDoc->m_tracker.m_rect.left,
  126.             pDoc->m_tracker.m_rect.top+nHeight/2, nWidth/2, (nHeight+nSgnY)/2);
  127.         pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
  128.         nCenterX = rect.left + rect.Width()/2;
  129.         nCenterY = rect.top + rect.Height()/2 - tm.tmHeight/2;
  130.         pDC->ExtTextOut(nCenterX, nCenterY, ETO_CLIPPED, rect, _T("3"), 1, NULL);
  131.  
  132.         brush2.CreateSolidBrush(RGB(192, 192, 192));
  133.         pDC->SelectObject(&brush2);
  134.         brush1.DeleteObject();
  135.         SetNormalRect(rect, pDoc->m_tracker.m_rect.left+nWidth/2,
  136.             pDoc->m_tracker.m_rect.top+nHeight/2,
  137.             (nWidth+nSgnX)/2, (nHeight+nSgnY)/2);
  138.         pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
  139.         nCenterX = rect.left + rect.Width()/2;
  140.         nCenterY = rect.top + rect.Height()/2 - tm.tmHeight/2;
  141.         pDC->ExtTextOut(nCenterX, nCenterY, ETO_CLIPPED, rect, _T("4"), 1, NULL);
  142.  
  143.         // cleanup DC
  144.         if (pOldBrush != NULL)
  145.             pDC->SelectObject(pOldBrush);
  146.         brush2.DeleteObject();
  147.  
  148.         // draw tracker on outside
  149.         pDoc->m_tracker.Draw(pDC);
  150.     }
  151.     CATCH_ALL(e)
  152.     {
  153.         if (pOldBrush != NULL)
  154.             pDC->SelectObject(pOldBrush);
  155.     }
  156.     END_CATCH_ALL
  157. }
  158.  
  159. /////////////////////////////////////////////////////////////////////////////
  160. // CTrackerView printing
  161.  
  162. BOOL CTrackerView::OnPreparePrinting(CPrintInfo* pInfo)
  163. {
  164.     // default preparation
  165.     return DoPreparePrinting(pInfo);
  166. }
  167.  
  168. void CTrackerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  169. {
  170.     // TODO: add extra initialization before printing
  171. }
  172.  
  173. void CTrackerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  174. {
  175.     // TODO: add cleanup after printing
  176. }
  177.  
  178.  
  179.  
  180.  
  181. /////////////////////////////////////////////////////////////////////////////
  182. // CTrackerView diagnostics
  183.  
  184. #ifdef _DEBUG
  185. void CTrackerView::AssertValid() const
  186. {
  187.     CView::AssertValid();
  188. }
  189.  
  190. void CTrackerView::Dump(CDumpContext& dc) const
  191. {
  192.     CView::Dump(dc);
  193. }
  194.  
  195. CTrackerDoc* CTrackerView::GetDocument() // non-debug version is inline
  196. {
  197.     return STATIC_DOWNCAST(CTrackerDoc, m_pDocument);
  198. }
  199.  
  200. #endif //_DEBUG
  201.  
  202. /////////////////////////////////////////////////////////////////////////////
  203. // CTrackerView update
  204.  
  205. void CTrackerView::OnUpdate(CView* /*pSender*/, LPARAM lHint, CObject* /*pHint*/)
  206. {
  207.     if (lHint == 0)
  208.     {
  209.         CRect rectTrue;
  210.         CTrackerDoc* pDoc = GetDocument();
  211.         pDoc->m_tracker.GetTrueRect(&rectTrue);
  212.         InvalidateRect(rectTrue);
  213.     }
  214.     else
  215.     {
  216.         InvalidateRect((CRect*)lHint);
  217.     }
  218. }
  219.  
  220. /////////////////////////////////////////////////////////////////////////////
  221. // CTrackerView message handlers
  222.  
  223. void CTrackerView::OnLButtonDown(UINT nFlags, CPoint point)
  224. {
  225.     CTrackerDoc* pDoc = GetDocument();
  226.     CRect rectSave;
  227.     pDoc->m_tracker.GetTrueRect(rectSave);
  228.     if (pDoc->m_tracker.HitTest(point) < 0)
  229.     {
  230.         // just to demonstrate CRectTracker::TrackRubberBand
  231.         CRectTracker tracker;
  232.         if (tracker.TrackRubberBand(this, point, pDoc->m_bAllowInvert))
  233.         {
  234.             MessageBeep(0); // beep indicates TRUE
  235.  
  236.             // see if rubber band intersects with the doc's tracker
  237.             CRect rectT;
  238.             tracker.m_rect.NormalizeRect(); // so intersect rect works
  239.             if (rectT.IntersectRect(tracker.m_rect, pDoc->m_tracker.m_rect))
  240.             {
  241.                 // if so, put resize handles on it (ie. select it)
  242.                 if (pDoc->m_tracker.m_nStyle & CRectTracker::resizeInside)
  243.                 {
  244.                     // swap from resize inside to resize outside for effect
  245.                     pDoc->m_tracker.m_nStyle &= ~CRectTracker::resizeInside;
  246.                     pDoc->m_tracker.m_nStyle |= CRectTracker::resizeOutside;
  247.                 }
  248.                 else
  249.                 {
  250.                     // just use inside resize handles on first time
  251.                     pDoc->m_tracker.m_nStyle &= ~CRectTracker::resizeOutside;
  252.                     pDoc->m_tracker.m_nStyle |= CRectTracker::resizeInside;
  253.                 }
  254.                 pDoc->SetModifiedFlag();
  255.                 pDoc->UpdateAllViews(NULL, (LPARAM)(LPCRECT)rectSave);
  256.                 pDoc->UpdateAllViews(NULL);
  257.             }
  258.         }
  259.     }
  260.     else if (pDoc->m_tracker.Track(this, point, pDoc->m_bAllowInvert))
  261.     {
  262.         // normal tracking action, when tracker is "hit"
  263.         pDoc->SetModifiedFlag();
  264.         pDoc->UpdateAllViews(NULL, (LPARAM)(LPCRECT)rectSave);
  265.         pDoc->UpdateAllViews(NULL);
  266.     }
  267.     CView::OnLButtonDown(nFlags, point);
  268. }
  269.  
  270.  
  271. BOOL CTrackerView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  272. {
  273.     // forward to tracker
  274.     CTrackerDoc* pDoc = GetDocument();
  275.     if (pWnd == this && pDoc->m_tracker.SetCursor(this, nHitTest))
  276.         return TRUE;
  277.  
  278.     return CView::OnSetCursor(pWnd, nHitTest, message);
  279. }
  280.  
  281.  
  282. void CTrackerView::OnViewSethandlesize()
  283. {
  284.     CTrackerDoc* pDoc = GetDocument();
  285.     CHandleSize dlg;
  286.     dlg.m_nHandleSize = pDoc->m_tracker.m_nHandleSize;
  287.     if (dlg.DoModal() == IDOK)
  288.     {
  289.         pDoc->m_tracker.m_nHandleSize = dlg.m_nHandleSize;
  290.         pDoc->UpdateAllViews(NULL);
  291.     }
  292. }
  293.  
  294. void CTrackerView::OnViewSetminimumsize()
  295. {
  296.     CTrackerDoc* pDoc = GetDocument();
  297.     CMinSize dlg;
  298.     dlg.m_nMinX = pDoc->m_tracker.m_sizeMin.cx;
  299.     dlg.m_nMinY = pDoc->m_tracker.m_sizeMin.cy;
  300.     if (dlg.DoModal() == IDOK)
  301.     {
  302.         pDoc->m_tracker.m_sizeMin.cx = dlg.m_nMinX;
  303.         pDoc->m_tracker.m_sizeMin.cy = dlg.m_nMinY;
  304.         pDoc->UpdateAllViews(NULL);
  305.     }
  306. }
  307.